// const SimpleProgressWebpackPlugin = require('simple-progress-webpack-plugin');
// const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
const { merge: WebpackMerge } = require('webpack-merge'); // webpack 5
const { rootPath, version, name } = require("./app.config.js");

module.exports = (src, dist, statics, options) => {
  const srcPath = rootPath(src);
  const distPath = rootPath(dist);
  const isSourceMap = options.devtool === "source-map";
  const isProduction = process.env.NODE_ENV === "production";

  return WebpackMerge({
    mode: process.env.NODE_ENV || "development",
    output: {
      publicPath: '',
      path: distPath,
      filename: `[name]-${version}.js`
    },
    resolve: {
      extensions: ['.js', '.jsx', '.es', '.css', '.less'],
      alias: {
        '@': rootPath('src')
      }
    },
    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          include: [srcPath],
          exclude: /(node_modules|bower_components)/,
          use: {
            loader: "babel-loader",
            options: {
              presets: ["@babel/preset-env", "@babel/preset-react"], // 模块
              // plugins: [
              //   "@babel/plugin-transform-runtime",
              //   ["@babel/plugin-proposal-decorators", { "legacy": true }],
              //   ["@babel/plugin-proposal-private-methods", { "loose": true }],
              //   ["@babel/plugin-proposal-private-property-in-object", { "loose": true }],
              //   ["@babel/plugin-proposal-class-properties", { "loose": true }]
              // ]
            }
          }
        },
        // {
        //   test: /\.(css|less)$/,
        //   include: [srcPath, rootPath('node_modules')],
        //   exclude: /\.module\.(css|less)$/,
        //   use: [
        //     isSourceMap ? "style-loader" : MiniCssExtractPlugin.loader,
        //     { loader: 'css-loader', options: { sourceMap: isSourceMap } },
        //     {
        //       loader: 'postcss-loader',
        //       options: {
        //         sourceMap: isSourceMap,
        //         options: {},
        //         postcssOptions: { plugins: [ "postcss-preset-env" ] }
        //       }
        //     },
        //     { loader: 'less-loader', options: { sourceMap: isSourceMap, javascriptEnabled: true, modifyVars: {} } },
        //   ],
        // },
        // {
        //   test: /\.module\.less$/,
        //   use: [
        //     isSourceMap ? "style-loader" : MiniCssExtractPlugin.loader,
        //     { loader: 'css-loader', options: { sourceMap: isSourceMap, modules: true } },
        //     {
        //       loader: 'postcss-loader',
        //       options: {
        //         sourceMap: isSourceMap,
        //         postcssOptions: { plugins: [ "postcss-preset-env" ] }
        //       }
        //     },
        //     { loader: 'less-loader', options: { sourceMap: isSourceMap, javascriptEnabled: true, modifyVars: {} } }
        //   ]
        // },
        // {
        //   test: /\.(woff|eot|ttf|svg)$/,
        //   include: [srcPath],
        //   loader: 'url-loader',
        //   options: { limit: 10, name: "assets/fonts/[name].[ext]" }
        // },
        // {
        //   test: /\.(png|jpg|jpeg|gif|ico)$/,
        //   include: [srcPath],
        //   loader: 'url-loader',
        //   options: { limit: 1, name: "assets/images/[name].[ext]" }
        // },
        // {
        //   test: /\.html$/,
        //   loader: "html-loader",
        //   options: { minimize: isProduction }
        // }
      ]
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: rootPath(statics, "index.html"),
        filename: "index.html"
      }),
      new CopyWebpackPlugin({
        patterns: [
          {
            from: rootPath(statics, 'assets'),
            to: rootPath(dist, 'assets')
          }
        ]
      }),
      // new webpack.BannerPlugin(`${name} v${version}\n\nCopyright (c) 2023 Innev.\nLicensed under the MIT License (MIT)`),
      // new SimpleProgressWebpackPlugin(),
      // new MiniCssExtractPlugin({
      //   filename: `[name]-${version}.css`
      // }),
      new CleanWebpackPlugin()
    ]
  }, options);
};
